package text.ui;

import java.io.*;
import java.util.*;
import javax.xml.parsers.*;

import org.xml.sax.*;
import org.xml.sax.SAXException;  
import org.xml.sax.SAXParseException;
import org.xml.sax.helpers.DefaultHandler;

public class LexiconParser extends DefaultHandler
{
	private SimpleErrorHandler errorHandle = new SimpleErrorHandler();
	
	private Hashtable hData;
	private Vector vRow;
	private StringBuffer strbBuffer;
		
	public LexiconParser()
	{
		super();
		hData = new Hashtable();
		vRow = new Vector();
		strbBuffer = new StringBuffer();
	}
	
	public static void main(String args[])
	{		
		try
		{			
			//create the parser
			LexiconParser myParser = new LexiconParser();

			try 
			{
				myParser.parseDoc(true, myParser, args[0]);
				//myParser.parseDoc(true, myParser, "lexicon.xml");
			} catch(Exception e) {
				e.printStackTrace();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	//---------- Asks the parser to validate the XML document against its DTD ----------//
	
	protected void parseDoc(boolean isValidating, DefaultHandler handler, String file)
	{
		try {
			//set up the parser
			SAXParserFactory saxParser = SAXParserFactory.newInstance();
			
			//create the parser for validating
			saxParser.setValidating(isValidating);
			saxParser.setNamespaceAware(false);
			
			SAXParser parser = saxParser.newSAXParser();

			parser.parse(new File(file), handler);
		} catch (Exception e) {
			System.out.println("Error while parsing the XML document!");
			e.printStackTrace();
		}
	}
	
	//---------- XML specific methods ---------- //
	
	public void characters(char[] ch, int start, int length) throws org.xml.sax.SAXException
	{			
		//System.out.println("characters: " + new String (ch, start, length));
		strbBuffer.append(ch, start, length);
	}
	
	public void startDocument() throws SAXException
	{
		//System.out.println("Reading Start of XML document.");
	}
	
	public void endDocument(String name) throws SAXException
	{
		//System.out.println("End of Element.");
	}
	
	public void startElement(String name, Attributes attList) throws org.xml.sax.SAXException 
	{
		//Don't add "lexicon" to the vector that would be passed to Lexicon
		//else, just add the other elements' names as they are processed by the parser
		if (name.equalsIgnoreCase("rootWord")) {
			
			//add the attributes (rootWord) as well
			for (int i = 0; i < attList.getLength(); i++)
			{
				String attVal  = attList.getValue(i);
				//vRow.add("id: "+attVal);
				hData.put(name + vRow.size(), attVal);
			}
		}
	}
	
	public void endElement(String name) throws org.xml.sax.SAXException
	{	
		//adds delimeters to the vector for later processing	
		if (name.equalsIgnoreCase("rootWord")) {
			//vRow.add("end rootWord");
			vRow.add(hData);
			hData = new Hashtable();
		}
		else if (name.equalsIgnoreCase("englishWord")) {
			//vRow.add("end englishWord");
			//vRow.add("english: "+strbBuffer.toString());
			hData.put("englishWord"+vRow.size(), strbBuffer.toString());
			strbBuffer = new StringBuffer();
		}
		else if (name.equalsIgnoreCase("filipinoWord")) {
			//vRow.add("end filipinoWord");
			//vRow.add("filipino: "+strbBuffer.toString());
			hData.put("filipinoWord"+vRow.size(), strbBuffer.toString());
			strbBuffer = new StringBuffer();
		}
		else;

	}
	
	public Vector getData() {
		Vector vData = null;
		
		if(vRow != null && vRow.size() > 0) {
			vData = vRow;
		}
		
 		return vData;
	}
	
	//----------  Handle the errors accordingly ---------- //
	
	public void warning(SAXParseException e) throws org.xml.sax.SAXException
	{
		errorHandle.warning(e);
	}
	
	public void error(SAXParseException e) throws org.xml.sax.SAXException
	{
		errorHandle.error(e);
	}
	
	public void fatalError(SAXParseException e) throws org.xml.sax.SAXException
	{
		errorHandle.fatalError(e);
	}
}